package com.cw.jeeyt.service.impl.process;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.process.ProcessLeaveBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.process.ProcessLeaveDao;
import com.cw.jeeyt.dao.model.process.ProcessLeaveDo;
import com.cw.jeeyt.service.converter.process.ProcessLeaveConverter;
import com.cw.jeeyt.service.intf.process.ProcessLeaveService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 请假流程业务实现类
 *
 * @author chenw
 * @date 2019-03-10 20:55:56
 */
@Service
public class ProcessLeaveServiceImpl implements ProcessLeaveService {

    private static final Logger logger = LoggerFactory.getLogger(ProcessLeaveServiceImpl.class);
    @Autowired
    private ProcessLeaveDao processLeaveDao;//请假流程数据库操作接口

    /**
     * 查询所有请假流程
     */
    @Override
    public List<ProcessLeaveBo> queryProcessLeaveList() throws Exception {
        logger.info("查询所有请假流程列表");
        return ProcessLeaveConverter.doList2BoList(processLeaveDao.queryProcessLeaveList());
    }

    /**
     * 分页查询请假流程
     *
     * @param processLeaveBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ProcessLeaveBo> queryByPage(ProcessLeaveBo processLeaveBo, Integer startNum, Integer pageSize,
        Order order) throws Exception {

        if (processLeaveBo == null) {
            logger.error("请假流程为空");
            return null;
        }
        PageInfo<ProcessLeaveDo> pageInfo = processLeaveDao.queryByPage(ProcessLeaveConverter.bo2Do(processLeaveBo),
            startNum, pageSize, order);
        return new PageInfo<>(pageInfo, ProcessLeaveConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询请假流程信息
     *
     * @return
     */
    @Override
    public ProcessLeaveBo getProcessLeaveById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return ProcessLeaveConverter.do2Bo(processLeaveDao.getById(id));
    }

    /**
     * 新增请假流程
     *
     * @param processLeaveBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ProcessLeaveBo processLeaveBo) throws Exception {

        if (processLeaveBo == null) {
            logger.error("请假流程为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ProcessLeaveDo processLeave = ProcessLeaveConverter.bo2Do(processLeaveBo);
        processLeave.setId(SequenceTool.nextId());
        processLeave.setCreateBy(userBo.getId());
        processLeave.setCreateDate(new Date());
        processLeave.setUpdateBy(userBo.getId());
        processLeave.setUpdateDate(new Date());
        Integer result = processLeaveDao.insert(processLeave);
        return result;
    }

    /**
     * 新增请假流程不为空的字段
     *
     * @param processLeaveBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ProcessLeaveBo processLeaveBo) throws Exception {

        if (processLeaveBo == null) {
            logger.error("请假流程为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ProcessLeaveDo processLeave = ProcessLeaveConverter.bo2Do(processLeaveBo);
        processLeave.setId(SequenceTool.nextId());
        processLeave.setCreateBy(userBo.getId());
        processLeave.setCreateDate(new Date());
        processLeave.setUpdateBy(userBo.getId());
        processLeave.setUpdateDate(new Date());
        Integer result = processLeaveDao.insertSelective(processLeave);
        return result;
    }

    /**
     * 更新请假流程
     *
     * @param processLeaveBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ProcessLeaveBo processLeaveBo) throws Exception {

        if (processLeaveBo == null) {
            logger.error("请假流程为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ProcessLeaveDo processLeave = ProcessLeaveConverter.bo2Do(processLeaveBo);
        processLeave.setUpdateBy(userBo.getId());
        processLeave.setUpdateDate(new Date());

        processLeaveDao.update(processLeave);
    }

    /**
     * 更新请假流程不为空的字段
     *
     * @param processLeaveBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ProcessLeaveBo processLeaveBo) throws Exception {

        if (processLeaveBo == null) {
            logger.error("请假流程为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ProcessLeaveDo processLeave = ProcessLeaveConverter.bo2Do(processLeaveBo);
        processLeave.setUpdateBy(userBo.getId());
        processLeave.setUpdateDate(new Date());

        processLeaveDao.updateSelective(processLeave);
    }

    /**
     * 删除请假流程
     *
     * @param processLeaveBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ProcessLeaveBo processLeaveBo) throws Exception {

        if (processLeaveBo == null) {
            logger.error("请假流程为空");
            return;
        }
        ProcessLeaveDo processLeave = ProcessLeaveConverter.bo2Do(processLeaveBo);
        processLeaveDao.delete(processLeave);
    }
}
